package mat.simul;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;
import javax.swing.border.*;

import beanclavier.*;
import com.gilles.*;

public class SimCoffretCommande extends JPanel implements ProcessPCF8574Event, Serializable
{
    private LcdDisplayI2c lcdDisplayI2c1 = new LcdDisplayI2c();
    private JPanel panneauAfficheur = new JPanel();
    private JPanel panneauClavier = new JPanel();
    private JToggleButton contactCAP = new JToggleButton();
    private int numeroAcces;
    private JLabel jLabelnumeroAcces = new JLabel();
    private SimClavier simClavier1 = new SimClavier();
    private JButton jButton1 = new JButton();
    private FaceAvant faceAvant = new FaceAvant();
    private BorderLayout borderLayout1 = new BorderLayout();
    private Border border1;

    public SimCoffretCommande()
    {
	this(0);
    }
    public SimCoffretCommande(int numeroAcces)
    {
	this.numeroAcces = numeroAcces;
	try
	{
	    jbInit();
	}
	catch(Exception e)
	{
	    e.printStackTrace();
	}
	lcdDisplayI2c1.setAdresseI2c(mat.Cablage.HD66717);
	simClavier1.setAdresseI2c(mat.Cablage.PCF8574_CLAVIER);
	jLabelnumeroAcces.setText(Integer.toString(numeroAcces));
    }

    public void setNumeroAcces(int numeroAcces)
    {
	this.numeroAcces = numeroAcces;
	jLabelnumeroAcces.setText(Integer.toString(numeroAcces));
    }

    public byte doProcess(int function, byte dataPort)
    {
	if (function == 0) //write
	{
	    boolean eclaire = (dataPort & mat.Cablage.MASQUE_BACKLIGHT) == mat.Cablage.MASQUE_BACKLIGHT;
	    lcdDisplayI2c1.setBacklight(eclaire);
	    boolean reset = (dataPort & mat.Cablage.MASQUE_RESET_HD66717) == 0;
	    lcdDisplayI2c1.setReset(reset);
	    //System.out.println("reset: " + reset);
	}
	else
	{
	    if (function == 1) // read
	    {

	    }
	}
	return dataPort;
    }

    private void jbInit() throws Exception
    {
	border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.lightGray,new Color(135, 175, 100),new Color(80, 82, 68),Color.gray);
	this.setLayout(borderLayout1);
	lcdDisplayI2c1.setDisplayRightBorder(6);
	lcdDisplayI2c1.setBorder(border1);
	lcdDisplayI2c1.setDisplayLeftBorder(6);
	lcdDisplayI2c1.setCellPixelHeight(2);
	lcdDisplayI2c1.setCellHeight(8);
	lcdDisplayI2c1.setContrast(9);
	lcdDisplayI2c1.setDisplayTopBorder(6);
	lcdDisplayI2c1.setBounds(new Rectangle(5, 10, 268, 61));
	panneauAfficheur.setBackground(Color.black);
	panneauAfficheur.setBounds(new Rectangle(41, 43, 279, 82));
	panneauAfficheur.setLayout(null);
	panneauClavier.setBackground(Color.black);
	panneauClavier.setBounds(new Rectangle(158, 280, 131, 171));
	panneauClavier.setLayout(null);
	contactCAP.setBackground(Color.black);
	contactCAP.setBorder(null);
	contactCAP.setBorderPainted(false);
	contactCAP.setContentAreaFilled(false);
	contactCAP.setFocusPainted(false);
	contactCAP.setIcon(new ImageIcon(SimCoffretCommande.class.getResource("carteoff.gif")));
	contactCAP.setSelectedIcon(new ImageIcon(SimCoffretCommande.class.getResource("carteon.gif")));
	contactCAP.setBounds(new Rectangle(101, 159, 231, 23));
	contactCAP.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		contactCAP_actionPerformed(e);
	    }
	});
	jLabelnumeroAcces.setBackground(Color.red);
	jLabelnumeroAcces.setFont(new java.awt.Font("Dialog", 1, 35));
	jLabelnumeroAcces.setForeground(Color.white);
	jLabelnumeroAcces.setOpaque(true);
	jLabelnumeroAcces.setHorizontalAlignment(SwingConstants.CENTER);
	jLabelnumeroAcces.setText("20");
	jLabelnumeroAcces.setBounds(new Rectangle(31, 281, 50, 38));
	simClavier1.setBounds(new Rectangle(5, 5, 120, 160));
	jButton1.setBackground(Color.blue);
	jButton1.setBounds(new Rectangle(316, 214, 20, 16));
	jButton1.setBorderPainted(false);
	jButton1.setContentAreaFilled(false);
	jButton1.setHorizontalTextPosition(SwingConstants.CENTER);
	jButton1.setIcon(new ImageIcon(SimCoffretCommande.class.getResource("gardienoff.jpg")));
	jButton1.setPressedIcon(new ImageIcon(SimCoffretCommande.class.getResource("gardienon.jpg")));
	jButton1.setText("boutonGardien");
	jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
	    public void mousePressed(MouseEvent e) {
		jButton1_mousePressed(e);
	    }
	    public void mouseReleased(MouseEvent e) {
		jButton1_mouseReleased(e);
	    }
	});
	faceAvant.setBackground(Color.lightGray);
	faceAvant.setMaximumSize(new Dimension(367, 516));
	faceAvant.setMinimumSize(new Dimension(367, 516));
	faceAvant.setOpaque(false);
	faceAvant.setPreferredSize(new Dimension(367, 516));
	faceAvant.setLayout(null);
	this.setMinimumSize(new Dimension(376, 516));
	this.setPreferredSize(new Dimension(367, 516));
	this.add(faceAvant, BorderLayout.CENTER);
	faceAvant.add(panneauClavier, null);
	panneauClavier.add(simClavier1, null);
	faceAvant.add(jLabelnumeroAcces, null);
	faceAvant.add(panneauAfficheur, null);
	panneauAfficheur.add(lcdDisplayI2c1, null);
	faceAvant.add(contactCAP, null);
	faceAvant.add(jButton1, null);
    }

    public  int write(int adresse, byte[] barr, int off, int len)
    {
	int temp;
	temp = lcdDisplayI2c1.write(adresse, barr, off, len);
	if (temp != -1) return temp;
	temp = simClavier1.write(adresse, barr, off, len);
	if (temp != -1) return temp;
	return -1;
    }

    public int read (int adresse, byte[] barr, int off, int len)
    {
	int temp;
	temp = lcdDisplayI2c1.read(adresse, barr, off, len);
	if (temp != -1) return temp;
	temp = simClavier1.read(adresse, barr, off, len);
	if (temp != -1) return temp;
	return -1;
    }

    void contactCAP_actionPerformed(ActionEvent e)
    {
	//System.out.println(this.getParent().getParent().getParent().getParent().getClass());
	((SimAccesParking)this.getParent().getParent().getParent().getParent()).doCarteIntroduite(contactCAP.isSelected());

    }

    void jButton1_mousePressed(MouseEvent e)
    {
	simClavier1.setToucheC(true);
    }

    void jButton1_mouseReleased(MouseEvent e)
    {
	simClavier1.setToucheC(false);
    }
    private void writeObject(ObjectOutputStream oos) throws IOException {
	oos.defaultWriteObject();
    }
    private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
	ois.defaultReadObject();
    }



}